package com.mazihao.emos.controller;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mazihao.emos.common.util.AjaxResult;
import com.mazihao.emos.config.shiro.JwtUtil;
import com.mazihao.emos.controller.form.DeleteMessageRefByIdForm;
import com.mazihao.emos.controller.form.SearchMessageByIdForm;
import com.mazihao.emos.controller.form.SearchMessageByPageForm;
import com.mazihao.emos.controller.form.UpdateUnreadMessageForm;
import com.mazihao.emos.db.pojo.TbMessage;
import com.mazihao.emos.db.pojo.TbMessageRef;
import com.mazihao.emos.service.MessageService;
import com.mazihao.emos.task.MessageTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/message")
@Api("消息管理")
public class MessageController {
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private MessageService messageService;
    @Resource
    private MessageTask messageTask;

    @PostMapping("/searchMessageByPage")
    @ApiOperation("分页查询消息")
    public AjaxResult searchMessageByPage(@RequestBody @Valid SearchMessageByPageForm form,@RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        int page = form.getPage();
        int length = form.getLength();
        PageHelper.startPage(page, length);
        List<TbMessage> list = messageService.searchMessageByPage(userId);
        for (TbMessage message : list) {
            message.setSendTime(DateUtil.offset(message.getSendTime(), DateField.HOUR, 8));
            TbMessageRef messageRef = messageService.searchMessageRefByMessageId(message.getId(), userId);
            message.setReceiverId(messageRef.getReceiverId());
            message.setRefId(messageRef.getId());
            message.setReadFlag(messageRef.getReadFlag());
            message.setLastFlag(messageRef.getLastFlag());
        }
        PageInfo<TbMessage> pageInfo = new PageInfo<>(list);
        return AjaxResult.ok().put("result", pageInfo);
    }

    @PostMapping("/searchMessageById")
    @ApiOperation("根据id查询消息")
    public AjaxResult searchMessageById(@RequestBody @Valid SearchMessageByIdForm form, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        TbMessage message = messageService.searchMessageById(form.getId());
        TbMessageRef messageRef = messageService.searchMessageRefByMessageId(message.getId(), userId);
        message.setReceiverId(messageRef.getReceiverId());
        message.setRefId(messageRef.getId());
        message.setReadFlag(messageRef.getReadFlag());
        message.setLastFlag(messageRef.getLastFlag());
        return AjaxResult.ok().put("result", message);
    }

    @PostMapping("/updateUnreadMessage")
    @ApiOperation("未读消息更新成已读消息")
    public AjaxResult updateUnreadMessage(@RequestBody @Valid UpdateUnreadMessageForm form, @RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        long rows = messageService.updateUnreadMessage(form.getId(), userId);
        return AjaxResult.ok().put("result", rows == 1);
    }
 
    @PostMapping("/deleteMessageRefById")
    @ApiOperation("根据id删除消息")
    public AjaxResult deleteMessageRefById(@RequestBody @Valid DeleteMessageRefByIdForm form) {
        long rows = messageService.deleteMessageRefById(form.getId());
        return AjaxResult.ok().put("result", rows == 1);
    }

    @PostMapping("/deleteUserMessageRef")
    @ApiOperation("根据userId删除接收人消息")
    public AjaxResult deleteUserMessageRef(@RequestHeader("token") String token) {
        int userId = jwtUtil.getUserId(token);
        long rows = messageService.deleteUserMessageRef(userId);
        return AjaxResult.ok().put("result", rows == 1);
    }

    @GetMapping("/refreshMessage")
    @ApiOperation("刷新用户消息")
    public AjaxResult refreshMessage(@RequestHeader("token") String token) throws Exception  {
        int userId = jwtUtil.getUserId(token);
        messageTask.receiveAsync(userId);
        long lastCount = messageService.searchLastCount(userId);
        long unreadCount = messageService.searchUnreadCount(userId);
        return AjaxResult.ok().put("lastRows", lastCount).put("unreadRows", unreadCount);
    }

}
